package org.brisling.medical.service;

import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.medical.domain.AntiMedical;
import org.brisling.medical.domain.MedicalRecipel;
import org.brisling.medical.queryService.QueryAntimedicalService;
import org.brisling.medical.repository.AntiMedicalRepo;
import org.brisling.sysman.domain.VOJobUser;
import org.brisling.sysman.queryService.QueryPermissionService;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/AntiMedical")
/**
 * 抗菌药物使用率CUD服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class AntiMedicalService extends BaseSysmanService<AntiMedical>{
	
	@SuppressWarnings("unused")
	@Autowired
	private AntiMedicalRepo baseRepo;
	
	public AntiMedicalService(){
		this.moduleName = ConstSysParam.MODULE_ANTI_MEDICAL;
		
	}

	@Autowired
	private RecipelService qryService;
	
	@Autowired
	private QueryAntimedicalService qryAntimedicalService;
	
	@Autowired
	private QueryPermissionService qryPermissionService;
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<AntiMedical> getListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<AntiMedical> getPagedListRows(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 删除组织节点
	 * @param id
	 */
	@RequestMapping("/save.action")
	public AntiMedical save(@RequestBody String body){
		AntiMedical obj = bodyToObj(body, AntiMedical.class);
		super.save(obj);
		return obj;
	}
	
	/**
	 * 批量保存
	 * @param lists
	 * @return
	 */
	public RetDomain save(List<AntiMedical> lists){
		RetDomain domain = new RetDomain();
		if(lists!=null && lists.size()>0){
			return super.saveList(lists);
		}else{
			domain.setErrorMsg("保存数据不能为空");
			return domain;
		}
	}
	
	/**
	 * 自动插入
	 * @param AntiMedical
	 */

	public void autoInsertData(){
		List<AntiMedical>  lists=qryAntimedicalService.getAntiMedicalAbnormal();
		String Bizcode = qryPermissionService.getBizFlowConfig("recipel").getFullBizcode();
		String flowid = qryPermissionService.getBizFlowConfig("recipel").getFlowid();
		if(lists!=null){
			for(AntiMedical a:lists){
				//System.out.println("\n__________________________________________________________________________________" + a.getDept_name() + "_________________________________________");
				if(!a.getData_type().equals("2")){
					//System.out.println("\nbad0000000000000000000000000000000000000000000000");
					continue;
				}
				//System.out.println("here, that have one");
				MedicalRecipel sendObj = new MedicalRecipel();
				sendObj.setInquiry_item("用药异常");
				sendObj.setPatient_type(a.getPatient_type());
				List<VOJobUser> jobUsr = SysConfig.getJobUserByDept("20", a.getDept_code().toString());
				sendObj.setDoct_name(jobUsr.get(0).getDoct_name());
				sendObj.setDoct_id(jobUsr.get(0).getDoct_code().toString());
				sendObj.setInquiry_item_code("6");
				String temp = a.getPatient_type();
				if(temp.equals("2")){
					sendObj.setInquiry_item_sub("住院抗菌药物使用率");
					sendObj.setInquiry_reason("住院抗菌药物使用率异常, 请予以合理解释");
					sendObj.setInquiry_item_subcode("12");
				}
				else{
					sendObj.setInquiry_item_sub("门急诊抗菌药物使用率");
					sendObj.setInquiry_reason("门急诊住院抗菌药物使用率异常, 请予以合理解释");
					sendObj.setInquiry_item_subcode("150");
				}
				sendObj.setDoct_depart_name(a.getDept_name());
				//sendObj.setDeptName(a.getDept_name());
				//System.out.println("\n--------------------------------------------------------------------------------------");
				//System.out.println(a.getDept_name());
				//sendObj.setDeptid(a.getDept_code());
				sendObj.setDoct_depart_code(a.getDept_code().toString());
				sendObj.setInquiry_date(new java.sql.Date(new java.util.Date().getTime()));
				sendObj.setFlowid(flowid);
				sendObj.setFullBizcode(Bizcode);
				//设置异常实体信息
				String id =SysConfig.getConfig("inquiryUserId");
				qryService.completeTask(id, sendObj);
			}
		}
	}
	
	/**
	 * 更新指标值
	 * @param year_month
	 */
	public int updateStandard(String year_month){
		String outsql = "update %1$s bm set (BM.AMOUNT_EVALUATE_TYPE_ID,BM.AMOUNT_PERCENT_STANDARD,BM.ACCOUNTING_DEPTID)= " +
					"(select I.INDICATOR_EVALUE_TYPE_ID,I.INDICATOR_VALUE,I.ACCOUNTING_DEPTID from %2$s i "+     
    				"where i.bizcode = 'in_antiDDD' and I.INDICATOR_PATIENT_TYPE_ID=BM.PATIENT_TYPE and I.INDICATOR_UNIT_ID=BM.DEPT_CODE ) "+
    				"where BM.MONITOR_YEAR_MONTH='%3$s'";
		outsql = String.format(outsql, this.moduleName,ConstSysParam.MODULE_INDICATION,year_month);
		return super.executeNativeDelOrUpdate(outsql);		
		
	} 
}
